#!/usr/bin/python
# -*- coding: utf-8 -*-

__all__ = [u'CliPart']

########## DO NOT CHANGE THESE LINES ###########################################
import sys
from pylot.core import (ENCODING, Path)
try : __file__
except NameError : co = sys._getframe(0).f_code ; __file__ = co.co_filename
ufile = __file__.decode(ENCODING)
if ufile.strip() : PLUGIN_PATH = Path(ufile).abspath().parent
else : PLUGIN_PATH = Path(u'.').abspath()
########## DO NOT CHANGE THESE LINES ###########################################

from optparse import make_option
from handler import HandlerPart

from pylot.core import ENCODING
from pylot.cli import CliInterface, pyLotOptionGroup

class CliPart(CliInterface):

  def __init__ (self, handler=None, parser=None):
    if handler is None :
      handler = HandlerPart()
      handler.init()
    CliInterface.__init__(self, handler=handler, parser=parser)

    self.version = '0.1'
    self.description = self.tr_(u'DESC')
    self.usage = 'prog [options]'

    self.i18n = self.addPlugin(u'tools_i18n', self.handler.i18n)
    self.testing = self.addPlugin(u'tools_testing', self.handler.testing)

    self._createOptions()

  def _createOptions(self):
    self.action_compile_all_translations = make_option('--compile-all-translations',
      action='store_true', dest='compile_all_translations')

    self.action_auto_write_plugin_ref = make_option('--write-plugin-ref',
      action='store', dest='auto_write_plugin_ref', metavar='PLUGIN_NAME')
    self.action_write_plugin_index = make_option('--write-plugin-index',
      action='store_true', dest='write_plugin_index')

    self.action_test_plugin = make_option('--test-plugin',
      action='store', dest='test_plugin', metavar=u'PLUGIN_NAME')
    self.action_check_plugin = make_option('--check-plugin',
      action='store', dest='check_plugin', metavar=u'PLUGIN_NAME')

  def _createOptionGroups(self):
    group = pyLotOptionGroup(self.parser, u'group_plugin_testing')
    self.group_plugin_testing = self.addGroup(group)
    self.addGroup(self.i18n.group_translate)
    self.addGroup(self.group_plugin_testing)
    self.addGroup(self.testing.group_testing)

  def fillGroups(self):
    self.i18n.fillGroups()
    self.testing.fillGroups()

    self.group_plugin_testing.add_option(self.action_test_plugin,
                                         userlist=self.parser.console)
    self.group_plugin_testing.add_option(self.action_check_plugin,
                                         userlist=self.parser.console)
    self.group_plugin_testing.add_option(self.action_auto_write_plugin_ref,
                                         userlist=self.parser.console)
    self.group_plugin_testing.add_option(self.action_write_plugin_index,
                                         userlist=self.parser.console)
    self.group_plugin_testing.add_option(self.action_compile_all_translations,
                                         userlist=self.parser.console)


  def _onActionTestPlugin(self):
    opt_value = self.options.test_plugin
    self.handler.testPlugin(opt_value.decode(ENCODING))

  def _onActionCheckPlugin(self):
    opt_value = self.options.check_plugin
    self.handler.checkPlugin(opt_value.decode(ENCODING))

  def _onActionWritePluginRef(self):
    opt_value = self.options.auto_write_plugin_ref
    self.handler.actionAutoWritePluginReference(opt_value.decode(ENCODING))

  def _runOptions(self, options):
    self.autoRunOption(self.action_test_plugin, self._onActionTestPlugin)
    self.autoRunOption(self.action_check_plugin, self._onActionCheckPlugin)
    self.autoRunOption(self.action_auto_write_plugin_ref,
                       self._onActionWritePluginRef)
    self.autoRunOption(self.action_write_plugin_index,
                       self.handler.actionWritePluginRstIndex)
    self.autoRunOption(self.action_compile_all_translations,
                       self.handler.actionCompileAllPluginTranslations)

  def _translate(self):
    self._setTexts([
      (self.group_plugin_testing, self.tr_(u'Plugin testing')),
      ], field=u'title')

if __name__ == '__main__':
  u"""
  Launching CLI
  """
  from _pylot.cli.tools.utils import launch_cli_module
  parser, cli, options = launch_cli_module(CliPart)
  cli.runOptions(options)
